package com.example.leetcode.leetcode.hot100;

import java.util.Arrays;

/**
 * @author haitao
 * @date 2021/8/27 11:22 上午
 */
public class Solution34 {

    public int[] searchRange(int[] nums, int target) {
        int n = nums.length;
        int l = 0, r = n - 1;
        Integer resultLeft = -1, resultRight = -1;
        resultLeft = recursion(nums, l, r, target, resultLeft, true);
        resultRight = recursion(nums, l, r, target, resultRight, false);
        return new int[]{resultLeft, resultRight};
    }

    public Integer recursion(int[] nums, int l, int r, int target, Integer res, boolean leftRight) {
        if (l > r){
            return res;
        }
        int mid = (l + r) / 2;
        if (nums[mid] == target) {
            res = mid;
            if (leftRight) {
                r = mid -1;
            }else{
                l = mid + 1;
            }
            return recursion(nums, l, r, target, res, leftRight);
        } else if (nums[mid] > target) {
            return recursion(nums, l, mid - 1, target, res, leftRight);
        } else {
            return recursion(nums, mid + 1, r, target, res, leftRight);
        }
    }

    public static void main(String[] args) {
        Solution34 solution34 = new Solution34();
        int[] nums = new int[]{5,7,7,8,8,10};
        int target = 6;
        System.out.println(Arrays.toString(solution34.searchRange(nums, target)));
    }
}
